package test

import (
	"appengine"
	"appengine/datastore"
	"entity"
	"fmt"
	"model"
	"net/http"
	"search"
	"strconv"
	"strings"
	"time"
)

func Test(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	strTime := r.URL.Path
	strTime = strings.Replace(strTime, "/test/", "", -1)

	var month string = "1"
	var day string = "1"
	var year string = "2000"

	paraArraySplit := strings.Split(strTime, "-")
	if len(paraArraySplit) == 4 {
		month = paraArraySplit[1]
		day = paraArraySplit[2]
		year = paraArraySplit[3]
	}

	var m time.Month
	var d int
	var y int

	switch month {
	case "1":
		m = 1
	case "2":
		m = 2
	case "3":
		m = 3
	case "4":
		m = 4
	case "5":
		m = 5
	case "6":
		m = 6
	case "7":
		m = 7
	case "8":
		m = 8
	case "9":
		m = 9
	case "10":
		m = 10
	case "11":
		m = 10
	case "12":
		m = 10
	default:
		m = 1
	}

	d, _ = strconv.Atoi(day)

	y, _ = strconv.Atoi(year)

	t := time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location())

	arr, _ := model.ListNewsByTime(c, t, 1)

	for _, e := range arr {
		fmt.Fprintln(w, e.Title+"/n")
	}
}

func TestDatastoreAddTag(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	var tag entity.Tag
	tag.Alias = "tag-01"
	tag.Title = "Tag Title 01"
	if err := model.AddTag(c, tag); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprint(w, "Success !")
	}
}

func TestDatastoreAddTagRelationship(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	var tagRelationship entity.TagRelationship

	tagRelationship.TagAlias = "tag-01"
	tagRelationship.NewsAlias = "Tag Title 01"
	if err := model.AddTagRelationship(c, tagRelationship); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprint(w, "Success !")
	}
}

func TestDatastoreGetTag(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("alias")
	c := appengine.NewContext(r)
	var obj entity.Tag
	if err := model.GetTag(c, id, &obj); err == datastore.ErrNoSuchEntity {
		http.Error(w, "No Suck Entity", http.StatusInternalServerError)
		return
	} else if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprint(w, "Title : "+obj.Title)
	}
}

func TestDatastoreAddNews(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	var news entity.News
	news.Alias = "test-teaser-02"
	news.Title = "Test Teaser"
	news.Description = "Test Description"
	news.Content = "Test Content"
	news.Source = "Test Source"
	news.Thumb = "Test Thumb"
	//	news.Category = "Category"
	//	news.CategoryAlias = "cate-alias"
	news.Doc = time.Now()
	news.Tags = []string{"Tag 01", "Tag 02", "Tag 03"}

	if err := model.AddNews(c, news); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprint(w, "Success !")
	}
}

func TestDatastoreGetNews(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")
	c := appengine.NewContext(r)
	var obj entity.News
	if err := model.GetNews(c, id, &obj); err == datastore.ErrNoSuchEntity {
		http.Error(w, "No Suck Entity", http.StatusInternalServerError)
		return
	} else if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprint(w, "Title : "+obj.Title)
	}
}

func TestDatastoreCheckExistNews(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")
	c := appengine.NewContext(r)
	if model.CheckExistNews(c, id) {
		fmt.Fprint(w, "Exist news !")
	} else {
		fmt.Fprint(w, "Not exist news !")
	}
}

func TestSearchAdd(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	var obj entity.NewsSearch
	obj.Alias = "stellar-marionette-fansign"
	obj.Title = "Stellar Marionette Fansign"
	obj.Description = "Girl group Stellar at a fansign event in February to promote Marionette album."
	obj.Content = "Girl group Stellar at a fansign event in February to promote Marionette album."
	//	obj.Category = "Actress"
	//	obj.CategoryAlias = "actress"
	obj.Doc = time.Now()
	obj.Tags = "Actress, Stellar"

	if err := search.AddNews(c, obj); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprintln(w, "Success !")
	}
}

func TestSearchQuery(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	arr, err := search.SearchNews(c, "Girl group Stellar", 1)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		for _, e := range arr {
			fmt.Fprintln(w, e.Title)
		}
	}
}

func TestSearchGet(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	var obj entity.NewsSearch
	err := search.GetNews(c, "park-bo-young-sudden-attack-champions-league", &obj)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprintln(w, obj.Title)
	}
}
func TestSearchDelete(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	err := search.DeleteNews(c, "park-bo-young-sudden-attack-champions-league")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		fmt.Fprintln(w, "Success !")
	}
}
